﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SockLib.Device
{
    /// <summary>
    /// 使用GPRS的方式发送JSON格式字符串
    /// </summary>
      class GPRS2JSON:JSON 
    {
        public override  bool DeviceReg(byte[] Buffer, ref DeviceModel DM, out List<DeviceModel.SendMsg> Sendinfo)
        {
            try
            {
                Sendinfo = new List<DeviceModel.SendMsg>();
                DeviceModel DeviceModel = new DeviceModel();
                byte[] RegMsg;
                if (!RegRull(Buffer, out RegMsg))
                {
                    return false;  //未能解析，返回失败
                }

                string rec = System.Text.Encoding.Default.GetString(Buffer);
                string[] reginfo = rec.Split('@');
                DeviceModel.Uid = reginfo[1];
                if (Convert.ToInt16(DeviceModel.Uid.Substring(0, 1)) == 1)  //第1位是1中心，其他节点
                    DeviceModel.IsCenter = true;
                else
                    DeviceModel.IsCenter = false;
                DeviceModel.DeviceGroup = DeviceModel.Uid.Substring(1, 4); //获取组号
                DeviceModel.MobileNo = reginfo[2];
                DM = DeviceModel;

                DeviceModel.SendMsg DS = new DeviceModel.SendMsg();
                DS.TargetUid = DeviceModel.Uid;
                DS.DeviceOper = Converts.DeviceOper.Register;
                DS.SendBuffer = RetRegByte(DeviceModel.Uid, "1");
                if (Convert.ToInt16(DeviceModel.Uid.Substring(0, 1)) == 1)  //第1位是1中心，其他节点
                {
                    DeviceModel.IsCenter = true;
                    Sendinfo.Add(DS);
                }
                else
                    DeviceModel.IsCenter = false;

                return true;   //返回注册成功
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private bool RegRull(byte[] Buffer, out byte[] reg)
        {
            //注册格式：JSON@8-11位编号#11位电话号码
            string rec = System.Text.Encoding.Default.GetString(Buffer);
            string[] reginfo = rec.Split('@');
            if (reginfo.Length == 3)
            {
                if (reginfo[0] != "JSON" || reginfo[2].Length != 11 || reginfo[1].Length < 8 || reginfo[1].Length > 11)
                {
                    reg = new byte[0];
                    return false;
                }
                else
                {
                    reg = System.Text.Encoding.Default.GetBytes(reginfo[1]);
                    return true;
                }
            }
            else
            {
                reg = new byte[0];
                return false;
            }
        }

        private byte[] RetRegByte(string gprsid, string success)
        {
            JSON_RetReg JReturn = new Device.JSON_RetReg();
            JReturn.gprs_code = gprsid;
            JReturn.success = success;
            string jsonstr = JS.Serialize(JReturn);
            byte[] returbyte = System.Text.Encoding.Default.GetBytes(jsonstr);
            return returbyte;
        }

        public  override  string DeviceType()
        {
            return "GPRS2JSON";
        }
    }
}
